package com.collect.cameroon.zhiScene.zhiWidget.zhiDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiUtils.StringUtil;

public class ZHIDialogNxcallLogin extends Dialog {

    private final Context mContext;

    private TextView tv_dnl_title, btn_dnl_negtive;

    private EditText et_dnl_account_input, et_dnl_password_input;

    private String mTitle;

    private String mAccount;

    private String mPassword;

    private ZHIDialogNxcallLoginListener mZHIDialogNxcallLoginListener;

    public static void showDialog(Context context, String title, String account, String password, ZHIDialogNxcallLoginListener listener) {
        if (context == null) {
            return;
        }
        ZHIDialogNxcallLogin zhiDialogNumberInput = new ZHIDialogNxcallLogin(context);
        zhiDialogNumberInput.mTitle = StringUtil.safeString(title);
        zhiDialogNumberInput.mAccount = StringUtil.safeString(account);
        zhiDialogNumberInput.mPassword = StringUtil.safeString(password);
        zhiDialogNumberInput.mZHIDialogNxcallLoginListener = listener;
        zhiDialogNumberInput.show();
    }

    public ZHIDialogNxcallLogin(Context context) {
        super(context, R.style.CustomDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_nxcall_login);
        initView();
    }

    @Override
    public void show() {
        super.show();
        refreshViewData();
    }

    private void initView() {
        tv_dnl_title = findViewById(R.id.tv_dnl_title);
        et_dnl_account_input = findViewById(R.id.et_dnl_account_input);
        et_dnl_password_input = findViewById(R.id.et_dnl_password_input);
        btn_dnl_negtive = findViewById(R.id.btn_dnl_negtive);

        btn_dnl_negtive.setOnClickListener(v -> {
            if (mZHIDialogNxcallLoginListener != null) {
                mZHIDialogNxcallLoginListener.onConfirmClick(
                        StringUtil.safeString(et_dnl_account_input.getText().toString().trim()),
                        StringUtil.safeString(et_dnl_password_input.getText().toString().trim()));
            }
            dismiss();
        });
    }

    private void refreshViewData() {
        if (!TextUtils.isEmpty(mTitle)) {
            tv_dnl_title.setText(mTitle);
        }
        if (!TextUtils.isEmpty(mAccount)) {
            et_dnl_account_input.setText(mAccount);
            et_dnl_account_input.setSelection(mAccount.length());
        }
        if (!TextUtils.isEmpty(mPassword)) {
            et_dnl_password_input.setText(mPassword);
            et_dnl_password_input.setSelection(mPassword.length());
        }
    }

}
